package com.zjitc.common;

import com.fasterxml.jackson.annotation.JsonInclude;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/3/27 16:54
 * @description:
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ServerResponse<T> {
    private Integer status;
    private String msg;
    private T data;

    public ServerResponse(Integer status, T arg) {
        this.status = status;
        this.data = arg;
    }

    public ServerResponse(Integer status) {
        this.status = status;
    }

    public ServerResponse(Integer status, String msg) {
        this.status = status;
        this.msg = msg;
    }

    public ServerResponse(Integer status, String msg, T data) {
    this.status = status;
    this.msg = msg;
    this.data = data;
    }

    public Integer getStatus() {
        return status;
    }

    public String getMsg() {
        return msg;
    }

    public T getData() {
        return data;
    }
    public static <T> ServerResponse<T> createSuccess(Object...args){
       return create(ServerResponseCode.SUCCESS,args);
    }
    public static <T> ServerResponse<T> createError(Object...args){
        return create(ServerResponseCode.ERROR,args);
    }
    public static <T> ServerResponse<T> create(Integer status,Object...args){
        if (args.length>2){
            throw new IllegalArgumentException("argument is more than 2:"+args.length);
        }
        if (args.length==1) {
            if (args[0] instanceof String) {
                return new ServerResponse(status, (String) args[0]);
            } else {
                return new ServerResponse<T>(status, (T) args[0]);
            }
        }
            if (args.length == 2) {
                return new ServerResponse<T>(status, (String) args[0], (T) args[1]);
            }
        return new ServerResponse(status);
    }
}
